<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class pro_surat_penyelenggara extends MY_Controller {
	
	function __construct()
	{
		parent::__construct(); 
		
		$this->load->model('pro_surat_penyelenggara_model', 'Penyelenggara');
		$this->secure_session();
	}
	
	function index(){

		$data['title'] = "Surat Izin Penyelenggara";
		viewByTemplate('pro_surat_penyelenggara/index', $data, 'grid');
	}

	function id_auto(){
		$query1 = $this->db->query("SELECT id_surat FROM (SELECT * FROM pro_surat_penyelenggara ORDER BY id_surat DESC) WHERE rownum <=1");
	 				if ($query1->row_array()) {
	 					$getquery1 = $query1->row_array();
	 					$rowcount1 = $getquery1['id_surat'];
	 					$autonum1 = $rowcount1 + 1;
	 				} else {
					$autonum1 = 1;
				}

		$data['id_surat_auto'] = $autonum1;

		$query = $this->db->query("SELECT no_surat FROM (SELECT * FROM pro_surat_penyelenggara ORDER BY no_surat DESC) WHERE rownum <= 1");
		$getquery = $query->row_array();

		if($getquery)
			$MaxID = $getquery['no_surat'];
		else
			$MaxID = "000";

		$NextID = (int) substr($MaxID,0,3);

		$NextID++;

		$bulan = date("m");
		$tahun = date("Y");

		$hasil = "";
			$iromawi =
			array("01"=>"I","02"=>"II","03"=>"III",
					"04"=>"IV","05"=>"V","06"=>"VI",
					  "07"=>"VII","08"=>"VIII","09"=>"IX",
					  	"10"=>"X","11"=>"XI","12"=>"XII"
				/*20=>"XX",30=>"XXX",40=>"XL",50=>"L",60=>”LX”,70=>”LXX”,80=>”LXXX”,
				90=>”XC”,100=>”C”,200=>”CC”,300=>”CCC”,400=>”CD”,500=>”D”,
				600=>”DC”,700=>”DCC”,800=>”DCCC”,900=>”CM”,1000=>”M”,
				2000=>”MM”,3000=>”MMM”*/);
			if(array_key_exists($bulan,$iromawi)){
				$hasil = $iromawi[$bulan];
			}
		
		$data['no_surat_auto'] = "IF.04.00/" ."____" . "/PRIM/".$hasil."/".$tahun;

		echo json_encode($data);
	}
	
	function getdata()
	{
		$this->secure_session();
		echo $this->Penyelenggara->GetJSON();
	}

	private function validation()
	{
		$error = new ErrorList();

		// Get Error Template
		$tpl_empty = $this->MessageTemplate("ER001");
		$tpl_duplicate = $this->MessageTemplate("ER002");
		// Validation
		
		if (post("no_surat") == '') {
			$error->Add("no_surat", "no_surat", $tpl_empty);
		} else if (post("mode") == "create" && intval($this->get_data("SELECT 1 FROM pro_surat_penyelenggara WHERE no_surat= '".@ereg_replace("[^a-zA-Z0-9_-]", "",@strtoupper(@trim(post("no_surat"))))."'")) > 0) {
			$error->Add("no_surat", "No Surat", $tpl_duplicate);
		}else if (post("nama_penyelenggara") == '') {
			$error->Add("nama_penyelenggara", "Nama Penyelenggara", $tpl_empty);
		}else if (post("alamat") == '') {
			$error->Add("alamat", "Alamat", $tpl_empty);
		}else if (post("diwakili_oleh") == '') {
			$error->Add("diwakili_oleh", "Diwakili Oleh", $tpl_empty);
		}else if (post("diwakili_oleh") == '') {
			$error->Add("no_telp", "No Telp". $tpl_empty);
		}else if (post("nama_kegiatan") == '') {
			$error->Add("nama_kegiatan", "Nama Kegiatan", $tpl_empty);
		}else if (post("tanggal_mulai_kegiatan") == '') {
			$error->Add("tanggal_mulai_kegiatan", "Tangal Mulai Kegiatan", $tpl_empty);
		}else if (post("tanggal_selesai_kegiatan") == '') {
			$error->Add("tanggal_selesai_kegiatan", "Tangal Selesai Kegiatan", $tpl_empty);
		}else if (post("pukul") == '') {
			$error->Add("pukul", "Pukul", $tpl_empty);
		}else if (post("tempat_kegiatan") == '') {
			$error->Add("tempat_kegiatan", "Tempat Kegiatan", $tpl_empty);
		}

		return $error;
	}
	
	function ajax_post()
	{
		$is_error = 0;
		$ls_response = "";
		$ls_genid = "";

		$error = $this->validation();
		try
		{
			if (count( $error->Get() ) > 0) { $is_error = 1; }
			else
			{
				// Fill Post Data
					$data	=  array(
					'id_surat' => $this->input->post('id_surat'),
					'no_surat' => $this->input->post('no_surat'),
					'nama_penyelenggara' => $this->input->post('nama_penyelenggara'),
					'alamat' => $this->input->post('alamat'),
					'diwakili_oleh' => $this->input->post('diwakili_oleh'),
					'no_telp' => $this->input->post('no_telp'),
					'nama_kegiatan' => $this->input->post('nama_kegiatan'),
					'tanggal_mulai_kegiatan' => $this->input->post('tanggal_mulai_kegiatan'),
					'tanggal_selesai_kegiatan' => $this->input->post('tanggal_selesai_kegiatan'),
					'pukul' => $this->input->post('pukul'),
					'tempat_kegiatan' => $this->input->post('tempat_kegiatan'),
					'catatan_kegiatan' => $this->input->post('catatan_kegiatan'),
					);

					$data['tanggal_mulai_kegiatan']		= ($data['tanggal_mulai_kegiatan'] == '' ? NULL : date_format3($data['tanggal_mulai_kegiatan'], 'YYYY-MM-DD'));
			   		$data['tanggal_selesai_kegiatan']	= ($data['tanggal_selesai_kegiatan'] == '' ? NULL : date_format3($data['tanggal_selesai_kegiatan'], 'YYYY-MM-DD'));
			    
				if (post("mode") == "create")
				{
					$this->Penyelenggara->Add($data);

					$ls_response = "Data Tersimpan";

				}
				else if (post("mode") == "edit")
				{
					
					$this->Penyelenggara->Update($data, array('id_surat'	=> $data['id_surat']));

					$ls_response = "Data Tersimpan [edited]";
				}
			}
		} catch(Exception $e) {
			$is_error = 1; $ls_response = $e->getMessage();
		}
		
		echo $this->PostResponse(json_encode(array(	'is_error'	=> $is_error,
													'fields'	=> $error->GetDictionary(),
													'msg'		=> $ls_response,
													'genid'		=> $ls_genid,
													'form_sender'=> post("form_sender"))
								));
	}

	function ajax_delete()
	{
		$ls_result	= "";
		$id_surat	= get('id_surat');
		
		if ($id_surat != "")
		{
			// Delete Single
			$arr_rule	= array('id_surat' => $id_surat);
			$ls_result = $this->Penyelenggara->DeleteSingle($arr_rule);

		}
		else
		{
		}
		
			
			$num_row = intval($this->data['num_row']['deleted']);
			
			for ($i = 0; $i < $num_row; $i++)
			{
				$row	= $this->data['deleted'][$i];
				
				$arr_rule	= array('id_surat' => $row['id_surat']);
				$ls_result .= $this->Penyelenggara->DeleteSingle($arr_rule);
			}
		echo $ls_result;
	}
}
?>